package com.newegg.datafeed.adserving.api;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.newegg.datafeed.adserving.api.annotation.AccessRequired;
import com.newegg.datafeed.adserving.domain.Task;
import com.newegg.datafeed.adserving.model.UserType;
import com.newegg.datafeed.adserving.request.TaskForm;
import com.newegg.datafeed.adserving.response.Group;
import com.newegg.datafeed.adserving.response.Page;
import com.newegg.datafeed.adserving.response.Response;
import com.newegg.datafeed.adserving.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="任务接口")
@RestController
@RequestMapping("/api/task")
public class TaskAPI extends BaseAPI{
	
	@Autowired
	TaskService taskService;

	@ApiOperation(value="发布任务", consumes="application/json", produces="application/json")
    @RequestMapping(value="/publish", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.USER})
    public Response<Void> publish(@RequestBody TaskForm taskForm) throws Exception {
		taskService.save(taskForm);
		return SUCCESS();
    }
	
	@ApiOperation(value="任务执行状态", consumes="application/json", produces="application/json")
    @RequestMapping(value="/status", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN})
    public Response<Map<String, Long>> status() throws Exception {
		return SUCCESS(taskService.status());
    }
	
	@ApiOperation(value="任务执行状态", consumes="application/json", produces="application/json")
    @RequestMapping(value="/statusByUser", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<Map<String, Long>> statusByUser(@RequestParam String username) throws Exception {
		return SUCCESS(taskService.statusByUser(username));
    }
	
	@ApiOperation(value="任务执行状态", consumes="application/json", produces="application/json")
    @RequestMapping(value="/statusByCompany", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<Map<String, Long>> statusByCompany(@RequestParam int company) throws Exception {
		return SUCCESS(taskService.statusByCompany(company));
    }
	
	@ApiOperation(value="查询用户的任务列表", consumes="application/json", produces="application/json")
    @RequestMapping(value="/pageByUser", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<Page<Task>> pageByUser(@RequestParam String username, 
    		@RequestParam(defaultValue="1") Integer pageIndex, @RequestParam(defaultValue="10") Integer pageSize) throws Exception {
		return SUCCESS(taskService.pageByUser(username, pageIndex, pageSize));
    }
	
	@ApiOperation(value="查询公司任务列表", consumes="application/json", produces="application/json")
    @RequestMapping(value="/pageByCompany", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<Page<Task>> pageByCompany(@RequestParam int company, 
    		@RequestParam(defaultValue="1") Integer pageIndex, @RequestParam(defaultValue="10") Integer pageSize) throws Exception {
		return SUCCESS(taskService.pageByCompany(company, pageIndex, pageSize));
    }
	
	
	@ApiOperation(value="根据月份分组", consumes="application/json", produces="application/json")
    @RequestMapping(value="/groupMonth", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<List<Group>> groupMonth() throws Exception {
		return SUCCESS(taskService.groupMonth());
    }
	
	@ApiOperation(value="根据月份分组(指定公司)", consumes="application/json", produces="application/json")
    @RequestMapping(value="/groupMonthByCompany", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<List<Group>> groupMonth(@RequestParam int company) throws Exception {
		return SUCCESS(taskService.groupMonthByCompany(company));
    }
	
	@ApiOperation(value="根据月份分组(指定业务员)", consumes="application/json", produces="application/json")
    @RequestMapping(value="/groupMonthByUser", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<List<Group>> groupMonth(@RequestParam String username) throws Exception {
		return SUCCESS(taskService.groupMonthByUser(username));
    }
	
	@ApiOperation(value="根据广告商分组", consumes="application/json", produces="application/json")
    @RequestMapping(value="/groupChannel", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<List<Group>> groupChannel() throws Exception {
		return SUCCESS(taskService.groupChannel());
    }
	
	@ApiOperation(value="根据月份分组(指定公司)", consumes="application/json", produces="application/json")
    @RequestMapping(value="/groupChannelByCompany", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<List<Group>> groupChannel(@RequestParam int company) throws Exception {
		return SUCCESS(taskService.groupChannelByCompany(company));
    }
	
	@ApiOperation(value="根据月份分组(指定业务员)", consumes="application/json", produces="application/json")
    @RequestMapping(value="/groupChannelByUser", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired
    public Response<List<Group>> groupChannel(@RequestParam String username) throws Exception {
		return SUCCESS(taskService.groupChannelByUser(username));
    }
}
